package com.edu.zju.scs.panacea.mapper.resource;

import com.edu.zju.scs.panacea.entity.query.PositionQuery;
import com.edu.zju.scs.panacea.entity.resource.Position;
import com.edu.zju.scs.panacea.entity.resource.Site;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @Author: Yue Liang
 * @Date: 2018-12-13 19:48
 */

@Repository
public interface PositionMapper {
    @Select("SELECT * FROM res_position WHERE dev_id = #{devId}")
    @Results(id="positionMap", value={
            @Result(id=true,property="devId",column="dev_id"),
            @Result(property="country",column="country"),
            @Result(property="province",column="province"),
            @Result(property="city",column="city"),
            @Result(property="district",column="district")})
    Position selectPosition(Long devId);


    @Select("SELECT DISTINCT province FROM res_position where dev_id in (select dev_id from res_loc)")
    List<String> selectLocProvinces();

    @Select("<script> " +
            "SELECT DISTINCT city FROM res_position where dev_id in (select dev_id from res_loc)" +
            " <if test='province != null'> and province = #{province} </if> " +
            "</script>")
    List<String> selectLocCities(PositionQuery positionQuery);


    @Select("<script> " +
            "SELECT DISTINCT district FROM res_position where dev_id in (select dev_id from res_loc)" +
            " <if test='province != null'> and province = #{province} </if> " +
            " <if test='city != null'> and city = #{city} </if> " +
            "</script>")
    List<String> selectLocDistricts(PositionQuery positionQuery);


    @Delete("DELETE FROM res_position where dev_id = #{devId}")
    long deletePosition(Long devId);

    @Insert("INSERT INTO res_position(dev_id, country, province, city, district) VALUES(#{devId}, #{country}, #{province}, #{city}, #{district})")
    long insertPosition(Position position);

    @Update({ "update res_position set country = #{country}, province = #{province}, city = #{city}, district = #{district} where dev_id = #{devId}" })
    long updatePosition(Position position);


}
